//
//  PriceSelectedView.m
//  Learn
//
//  Created by zyl on 2024/4/3.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "PriceSelectedView.h"

@implementation PriceSelectedView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.selectRow = 0;
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collection = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
        self.collection.dataSource = self;
        self.collection.delegate = self;
        self.collection.y = 0;
        [self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self addSubview:self.collection];
    }
    return self;
}
- (void)setData:(NSArray *)data
{
    _data = data;
    [self.collection reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.data.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell removeAllSubviews];
    double width = (lScreenWidth - 20 - 40)/3.00;
    double height = 72;
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, width, height)];
    [cell addSubview:bgView];
    UIColor *colorBg = rgba(245, 245, 245, 1);
    UIColor *colorBorder = rgba(229, 229, 229, 1);
    UIColor *colorDot = rgba(229, 229, 229, 1);
    UIColor *colorTitle = rgba(142, 142, 142, 1);
    UIColor *colorPrice = rgba(51, 51, 51, 1);
    if (indexPath.row == self.selectRow) {
       colorBg = rgba(232, 253, 244, 1);
       colorBorder = rgba(12, 183, 108, 1);
       colorDot = rgba(12, 183, 108, 1);
       colorTitle = rgba(12, 183, 108, 1);
       colorPrice = rgba(12, 183, 108, 1);
    }
    
    [bgView setAllCornersWithCornerRadius:10];
    bgView.layer.borderColor = colorBorder.CGColor;
    bgView.layer.borderWidth = 1;
    
    drawDotLine(CGRectMake(0,(height - 1)/2.0, width,1), bgView, colorDot);
    bgView.backgroundColor = colorBg;
    NSDictionary *dic = self.data[indexPath.row];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, width,self.height/2.0)];
    titleLabel.font = RFontWithSize(14);
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    titleLabel.textColor = colorTitle;
        if (dic[@"mouth"]) {
            titleLabel.text = [NSString stringWithFormat:@"%d个月",[dic[@"mouth"] intValue]];
        }
    UILabel * priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,self.height/2.0, width,self.height/2.0)];
    priceLabel.font = MFontWithSize(16);

    if (dic[@"price"]) {
      priceLabel.text = [NSString stringWithFormat:@"¥%.2f",[dic[@"price"] doubleValue]];
    }
    [priceLabel setTextAlignment:NSTextAlignmentCenter];
    priceLabel.textColor = colorPrice;
    [cell addSubview:titleLabel];
    [cell addSubview:priceLabel];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.selectRow) {
        return;
    }
    self.selectRow = indexPath.row;
    [self.collection reloadData];
    if (self.block) {
       
        self.block(indexPath.row);
    }
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
     double width = (lScreenWidth - 20 - 40)/3.00;
    return CGSizeMake(width, 72);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 10, 0, 10);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
@end
